package com.foodchain.controller;

import com.foodchain.entity.User;
import com.foodchain.service.RedisService;
import com.foodchain.service.UserService;
import com.foodchain.util.IsPhone;
import com.foodchain.util.Md5SaltTool;
import com.foodchain.util.Misc;
import com.foodchain.vo.Register;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.persistence.Convert;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

    @Resource
    private UserService baseUserService;
    /*
     * 功能描述:用户登陆
     * @param:  * @param request
     * @param phone
     * @param password
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/27 12:56
     */
    @ResponseBody
    @RequestMapping("/login")
    public String login(HttpServletRequest request,String phone, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        //判断是否是手机端发送请求
        boolean b = IsPhone.JudgeIsMoblie(request);
        if(b){
            Map map = new HashMap<>();
            User user = baseUserService.findByUserName(phone);
            if(Misc.isEmpty(user)){
                Register register = new Register();
                register.setUserName(phone);
                User users = baseUserService.saveRegisterUser(register);
                map.put("userId", users.getId());
                return JsonCode(0,"success",map);
            }

            map.put("userId", user.getId());
            return JsonCode(0,"success",map);
        }
        User pcUser = baseUserService.findByUserName(phone);
        if(Misc.isNotEmpty(pcUser)){
          if(pcUser.getPassWord()!=null){
              boolean b1 = Md5SaltTool.validPassword(password, pcUser.getPassWord());
              if(b1){
                  return JsonCode(0,"success",pcUser);
              }
              return JsonCode(1,"密码输入错误",null);
          }
            return JsonCode(1,"请设置密码",null);
        }
        return JsonCode(1,"请先注册",null);
    }
    /*
     * 功能描述:根据积分倒序查询用户排名
     * @param:  * @param page
     * @param size
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/25 14:34
     */
    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestParam(value = "page",defaultValue = "0") Integer page,
                       @RequestParam(value = "size",defaultValue = "10") Integer size){
        Page<User> date = baseUserService.list(page,size);
        return JsonCode(0,"success",date);
    }
    /*
     * 功能描述:查询用户积分信息
     * @param:  * @param userId
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/27 15:44
     */
    @RequestMapping("/getUserIntegral")
    @ResponseBody
    public String getUserIntegral(String userId){
        User user = baseUserService.getByUserId(userId);
        return JsonCode(0,"success",user);
    }
}
